#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>

const std::string sep = ": ";
class Dict
{
private:
    std::string _filename;
    int _fd;
    std::unordered_map<std::string, std::string> _dict;

public:
    Dict(std::string filename = "Dict.txt")
        :_filename(filename) 
    {
        std::ifstream fin(_filename);
        if (!fin.is_open())
        {
            std::cout << "打开文件失败" << std::endl;
            exit(1);
        }

        std::string tmp;
        while (std::getline(fin, tmp))
        {
            if (tmp.empty())
                continue;
            int pos = tmp.find(sep, 0);
            if (pos ==std::string::npos)
                continue;
            std::string key = tmp.substr(0, pos);
            std::string value = tmp.substr(pos + sep.size());
            _dict.insert({key, value});
        }
        fin.close();
    }
    std::string Translate(const std::string& key)
    {
        auto it = _dict.find(key);
        if (it != _dict.end())
        {
            return it->second;
        }
        else 
        {
            return "UNKNOWN";
        }
    }
    ~Dict() {}
};